You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const Person = require('../../../models/person');
  2. import { NextApiRequest, NextApiResponse } from 'next';
  3. import dbConnect from '../../../utils/helpers/dbHelpers';
  4. import {
  5. IPerson,
  6. SPersonResponse,
  7. } from '../../../utils/interface/personInterface';
  8. async function handler(
  9. req: NextApiRequest,
  10. res: NextApiResponse<SPersonResponse>
  11. ) {
  12. const { method } = req;
  13. await dbConnect();
  14. switch (method) {
  15. case 'GET': {
  16. try {
  17. const dataId = req.query.dataId!.toString();
  18. const person: IPerson = await Person.findOne({ customID: dataId });
  19. if (!person) {
  20. throw new Error('Person with this id does not exist!');
  21. }
  22. res.status(200).json({
  23. message: 'Fetch single data successfull!',
  24. singleData: person,
  25. });
  26. } catch (error) {
  27. if (error instanceof Error)
  28. res.status(400).json({ message: error.message });
  29. else res.status(400).json({ message: 'Unexpected error' + error });
  30. }
  31. break;
  32. }
  33. default:
  34. res.status(405).json({ message: 'Method not allowed' });
  35. break;
  36. }
  37. }
  38. export default handler;